package pl.game.map;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import pl.game.MapView;
import pl.game.R;
import pl.game.R.drawable;
import pl.game.question.Question;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;

@Root
public class Tile {

	@Element
	private int id;
	@Element
	private String name;

	@Element
	private String flagBitmap;
	
	@ElementList
	private List<Integer> neighbours;

	private Bitmap bitmap;
	@Element
	private int x;
	@Element
	private int y;

	private ArrayList <Tile> neighbors;
	private ArrayList <Question> guestions;
	
	private int width;
	private int height;
	private boolean clicked;
	private boolean captured;
	private MapView mapView;
	
	private int points;
	
	public Tile(){}
	
	public Tile(int tileX, int tileY, Bitmap tileBitmap, boolean captured, MapView mapView){
        this.width = tileBitmap.getWidth();
        this.height = tileBitmap.getHeight();
		this.x = tileX;
		this.y = tileY;
		this.bitmap = tileBitmap;
		this.neighbors = new ArrayList<Tile>();
		this.clicked = false;
		this.captured = captured;
		this.mapView = mapView;
		this.points = 100;
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	
	public void setPoints(int points){
		this.points = points;
	}
	
	public int getPoints(){
		return this.points;
	}
	
	public void addNeighbor(Tile tile){
		this.neighbors.add(tile);
	}
	
	public void setNeighbors(ArrayList<Tile> neighbors) {
		this.neighbors = neighbors;
	}

	public ArrayList<Tile> getNeighbors(){
		return neighbors;
	}
	
	public void setCaptured(boolean captured){
		this.captured = captured;
	}
	
	public boolean getCaptured(){
		return this.captured;
	}
	
	
	public String getFlagBitmap() {
		return flagBitmap;
	}

	public void setFlagBitmap(String flagBitmap) {
		this.flagBitmap = flagBitmap;
	}

	public MapView getMapView() {
		return mapView;
	}

	public void setMapView(MapView mapView) {
		this.mapView = mapView;
	}

	public List<Integer> getNeighbours() {
		return neighbours;
	}

	public void setNeighbours(List<Integer> neighbours) {
		this.neighbours = neighbours;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void doDraw(Canvas canvas, float shiftX, float shiftY){
		canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2)+ shiftX, y - (bitmap.getHeight() / 2)+ shiftY, null);
		if(this.captured)
			canvas.drawBitmap(BitmapFactory.decodeResource(mapView.getResources(), R.drawable.checked2), x - 20 + shiftX, y - 20 + shiftY, null);
	}
	
	public void doDrawPaths(Canvas canvas, float shiftX, float shiftY ){
		Paint paint = new Paint();
		paint.setColor(Color.DKGRAY);
		paint.setStrokeCap(Paint.Cap.BUTT);
		paint.setStrokeWidth(8.0f);
		paint.setAntiAlias(true);
		Paint paint2 = new Paint();
		paint2.setColor(Color.BLACK);
		paint2.setStrokeCap(Paint.Cap.BUTT);
		paint2.setStrokeWidth(5.0f);
		paint2.setAntiAlias(true);
		
		Paint paint3 = new Paint();
		paint3.setColor(Color.WHITE);
		paint3.setStrokeCap(Paint.Cap.BUTT);
		paint3.setStrokeWidth(2.0f);
		paint3.setAntiAlias(true);
		paint3.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 1));
		
		if(clicked){
			for (Tile neighbor : neighbors) {
				if(!neighbor.captured){
				float[] pts = new float[]{x + shiftX, y+ shiftY, neighbor.getX() + shiftX, neighbor.getY() + shiftY};
				canvas.drawLines(pts, paint);
				canvas.drawLines(pts, paint2);
				canvas.drawLines(pts, paint3);
				}
			}
		}
	}
	
	public void Clicked(){
		if(this.clicked)
			clicked = false;
		else
			this.clicked = true;
	}
	
	public void UnClick(){
		this.clicked = false;
	}
	
    public boolean isCollition(float x2, float y2, float shiftX, float shiftY) {
        return x2 > x-width/2 + shiftX && x2 < x + width/2 + shiftX && y2 > y-height/2 + shiftY && y2 < y + height/2 +shiftY;  
  }

	public void setClicked(boolean b) {
		this.clicked = b;
		
	}

	public void setHeight(int height2) {
		this.height = height2;
		
	}

	public void setWidth(int width2) {
		this.width = width2;
		
	}
}